
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class UserListPanel : BasePanel
    {
        public ScrollRect scroll;
        public GameObject UserNamePrefab;
        public Button btnOK;
        public Button btnCancel;
        public Button btnAddUser;
        public Button btnRename;
        public Button btnRemove;
        private Dictionary<string, UserNameItem> menuNameItems = new Dictionary<string, UserNameItem>();
        private string curUserName;

        public string CurName
        {
            get { return curUserName; }
            set
            {
                curUserName = value;
                RefreshSelectState();
            }
        }

        public void RefreshSelectState()
        {
            // 刷新所有子节点的选中状态
            foreach (UserNameItem item in menuNameItems.Values)
            {
                item.RefreSelect();
            }
        }

        private void Awake()
        {
            btnOK.onClick.AddListener(OnBtnOK);
            btnCancel.onClick.AddListener(OnBtnCancel);
            btnAddUser.onClick.AddListener(OnBtnAddUser);
            btnRename.onClick.AddListener(OnBtnRename);
            btnRemove.onClick.AddListener(OnBtnRemove);
        }

        private void OnBtnRename(){
            if(CurName == DataManager.LoadClientData().curUserName){
                UIManager.Instance.OpenTips("不可修改当前用户");
                return;
            }
            BasePanel basePanel = UIManager.Instance.OpenPanel(UIConst.EditUserPanel);
            basePanel.GetComponent<EditUserPanel>().CurName = curUserName;
        }

        private void OnBtnRemove(){
            if(curUserName == DataManager.LoadClientData().curUserName){
                UIManager.Instance.OpenTips("不可删除当前用户");
                return;
            }
            DataManager.ClearUserData(curUserName);
        }

        private void Start()
        {
            curUserName = DataManager.LoadClientData().curUserName;
            RefreshMainPanel();
            RefreshSelectState();
        }

        // 注册事件监听器
        private void OnEnable()
        {
            UserEvents.onUserList += ChangeUserList;
        }

        // 注销事件监听器
        private void OnDisable()
        {
            UserEvents.onUserList -= ChangeUserList;
        }
        private void ChangeUserList() 
        {
            RefreshMainPanel();
        }


        //刷新列表
        public void RefreshMainPanel()
        {
            menuNameItems.Clear();
            //删除原来数据
            foreach (Transform child in scroll.content)
            {
                Destroy(child.gameObject);
            }
            Dictionary<string, UserData> usersData = DataManager.Load().usersData;
            foreach (string type in usersData.Keys)
            {
                UserData userData = usersData[type];
                Transform prefab = Instantiate(UserNamePrefab).transform;
                prefab.SetParent(scroll.content, false);
                prefab.GetComponent<UserNameItem>().Init(userData, this);
                menuNameItems.Add(userData.name, prefab.GetComponent<UserNameItem>());
            }

        }

        public void OnBtnOK()
        {
            DataManager.SaveClientData(new ClientData(curUserName));
            ClosePanel();
        }

        public void OnBtnCancel()
        {
            ClosePanel();
        }

        public void OnBtnAddUser()
        {
            UIManager.Instance.OpenPanel(UIConst.AddUserPanel);
        }
    }
}
